package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	s := "abc我爱中国"

	fmt.Println(len(s))

	fmt.Printf("%s \n", []byte(s))

	for _, b := range []byte(s) {
		fmt.Printf("%X ", b)
	}
	fmt.Println()

	for i, ch := range s { // ch is a rune
		fmt.Printf("(%d %X) ", i, ch) // 这里的下标遇到中文是会跳的
	}
	fmt.Println()

	// 获取字符串长度
	fmt.Println(utf8.RuneCountInString(s))

	// 读字符串
	bytes := []byte(s)
	for len(bytes) > 0 {
		ch, size := utf8.DecodeRune(bytes)
		bytes = bytes[size:]
		fmt.Printf("%c ", ch)
	}
	fmt.Println()

	// 用下标获取字符串
	for i, ch := range []rune(s) { // ch is a rune
		fmt.Printf("(%d %c) ", i, ch)
	}
	fmt.Println()
}
